package com.heima.xxljob.job;

import com.heima.xxljob.config.XxlJobConfig;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JianKang
 * @create 2023-11-02 14:18
 */

@Component
@Slf4j
public class HelloJob {

    @Value("${server.port}")
    private String port;

    @XxlJob("demoJobHandler")
    public void helloJob() {
        System.out.println("简单任务执行了，port=" + port);
    }

    @XxlJob("shardingJobHandler")
    public void shardingJobHandler() {
        //1.分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        log.info("shardIndex：{}", shardIndex);
        log.info("shardTotal：{}", shardTotal);

        for (Integer i : getList()) {
            if (i % shardTotal == shardIndex) {
                log.info("当前第{}分片执行了，任务项为：{}", shardIndex, i);
            }
        }

//        System.out.println("简单任务执行了，port=" + port);
    }

    public List<Integer> getList() {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}
